using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using Baiken.CaTa.Properties;

namespace Baiken.CaTa
{
    public partial class SystemSettings : Form
    {
        private SpeechRecognitionEngine recognizer;


        public SystemSettings()
        {
            InitializeComponent();

            initializeVR();
        }

        private void initializeVR()
        {
            recognizer = new SpeechRecognitionEngine();
            Choices programChoices = new Choices();

            programChoices.Add(Settings.Default.cata_command_previous, Settings.Default.cata_command_start, Settings.Default.cata_command_next);

            Grammar dictationGrammar = new Grammar(new GrammarBuilder(programChoices));

            dictationGrammar.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(dictationGrammar_SpeechRecognized);

            recognizer.LoadGrammar(dictationGrammar);


            recognizer.SetInputToDefaultAudioDevice();

            recognizer.RecognizeAsync(RecognizeMode.Multiple);
        }

        void dictationGrammar_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (Visible)
            {
                MethodInvoker methodInvoker = new MethodInvoker(delegate
                {
                    lConfidence.ForeColor = Color.Red;
                    lConfidence.Text = string.Format("Confidence: {0}", e.Result.Confidence);

                });
                if (InvokeRequired)
                {
                    Invoke(methodInvoker);
                }
                else
                {
                    methodInvoker.DynamicInvoke();
                }
            }

            if (e.Result.Confidence > (double)Settings.Default.cata_command_confidence)
            {
                if (Visible)
                {
                    Console.Beep();

                    MethodInvoker methodInvoker = new MethodInvoker(delegate
                    {
                        lConfidence.ForeColor = Color.Green;

                        if (e.Result.Text.Contains(Settings.Default.cata_command_next))
                        {
                            tbCommandNext.SelectAll();
                            tbCommandNext.Focus();
                        }
                        else if (e.Result.Text.Equals(Settings.Default.cata_command_previous))
                        {
                            tbCommandPreviuos.SelectAll();
                            tbCommandPreviuos.Focus();
                        }
                        else if (e.Result.Text.Equals(Settings.Default.cata_command_start))
                        {
                            tbCommandStart.SelectAll();
                            tbCommandStart.Focus();
                        }
                    });

                    if (InvokeRequired)
                    {
                        Invoke(methodInvoker);
                    }
                    else
                    {
                        methodInvoker.DynamicInvoke();
                    }

                }
                else
                {
                    if (e.Result.Text.Contains(Settings.Default.cata_command_next))
                    {
                        SendKeys.SendWait("{RIGHT}");
                    }
                    else if (e.Result.Text.Equals(Settings.Default.cata_command_previous))
                    {
                        SendKeys.SendWait("{LEFT}");

                    }
                    else if (e.Result.Text.Equals(Settings.Default.cata_command_start))
                    {
                        SendKeys.SendWait("{F5}");

                    }
                }
                Console.WriteLine(e.Result.Confidence + " :: " + e.Result.Text);

            }
        }

        protected override void OnClosed(EventArgs e)
        {
            finalizeVR();
            base.OnClosed(e);
        }

        private void finalizeVR()
        {
            recognizer.UnloadAllGrammars();
            recognizer.Dispose();
            recognizer = null;
        }

        private void SystemSettings_Load(object sender, EventArgs e)
        {
            tbCommandNext.Text = Settings.Default.cata_command_next;
            tbCommandPreviuos.Text = Settings.Default.cata_command_previous;
            tbCommandStart.Text = Settings.Default.cata_command_start;
            nudConfidence.Value = Settings.Default.cata_command_confidence;
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want exit the CaTa?", "CaTa", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Environment.Exit(0);
            }
        }


        private void SystemSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            WindowState = FormWindowState.Minimized;
            Hide();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();
            Activate();
        }

        private void SystemSettings_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                lConfidence.ForeColor = Color.Red;
                lConfidence.Text = "Confidence: ?.???";

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Settings.Default.cata_command_next = tbCommandNext.Text.Trim();
            Settings.Default.cata_command_previous = tbCommandPreviuos.Text.Trim();
            Settings.Default.cata_command_start = tbCommandStart.Text.Trim();
            Settings.Default.cata_command_confidence = nudConfidence.Value;

            Settings.Default.Save();

            finalizeVR();
            initializeVR();
        }

    }
}